#!/bin/sh

cd ca-server
echo "IntermediaCA2生成证书"
echo "1.生成org1.example.com的msp"
echo "1）登记org1.example.com"
fabric-ca-client enroll --csr.cn=org1.example.com --csr.hosts=['org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
cp ../config.yaml ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/msp/config.yaml
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/msp/config.yaml
fabric-ca-client enroll --csr.cn=org1.example.com --csr.hosts=['org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
echo "2）添加联盟成员"
fabric-ca-client affiliation list -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org1 -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org2 -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
fabric-ca-client affiliation add com -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
fabric-ca-client affiliation add com.example -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
fabric-ca-client affiliation add com.example.org1 -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client


echo "2.生成Admin@example.com的msp"
echo "1）注册Admin@example.com"
fabric-ca-client register --id.name Admin@org1.example.com --id.type client --id.affiliation "com.example.org1" --id.attrs '"hf.Registrar.Roles=client,orderer,peer,user","hf.Registrar.DelegateRoles=client,orderer,peer,user",hf.Registrar.Attributes=*,hf.GenCRL=true,hf.Revoker=true,hf.AffiliationMgr=true,hf.IntermediateCA=true,role=admin:ecert' --id.secret=123456 --csr.cn=org1.example.com --csr.hosts=['org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
echo "2）登记Admin@example.com"
fabric-ca-client enroll -u http://Admin@org1.example.com:123456@localhost:7056 --csr.cn=org1.example.com --csr.hosts=['org1.example.com']  -M ./crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp --home ./fabric-ca-client

cp ../config.yaml ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/config.yaml
echo "3）生成msp"
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/admincerts
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/cert.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/admincerts/Admin@org1.example.com-cert.pem
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/msp/admincerts
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/cert.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/msp/admincerts/Admin@org1.example.com-cert.pem


echo "3.生成peer0.org1.example.com的msp"
echo "1）注册peer0.org1.example.com"
fabric-ca-client register --id.name peer0.org1.example.com --id.type peer --id.affiliation "com.example.org1" --id.attrs '"role=peer",ecert=true' --id.secret=123456 --csr.cn=peer0.org1.example.com --csr.hosts=['peer0.org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/msp -u http://admin2:adminpw2@localhost:7056 --home ./fabric-ca-client
echo "2）登记peer0.org1.example.com"
fabric-ca-client enroll -u http://peer0.org1.example.com:123456@localhost:7056 --csr.cn=peer0.org1.example.com --csr.hosts=['peer0.org1.example.com']  -M ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp --home ./fabric-ca-client
echo "3）生成msp"
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/admincerts
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/cert.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/admincerts/Admin@org1.example.com-cert.pem


echo "IntermediaCAtls2生成证书"
echo "1.生成org1.example.com的tls"
echo "1）登记org1.example.com"
fabric-ca-client enroll --csr.cn=org1.example.com --csr.hosts=['org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
echo "2）添加联盟成员"
fabric-ca-client affiliation list -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org1 -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org2 -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
fabric-ca-client affiliation add com -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
fabric-ca-client affiliation add com.example -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
fabric-ca-client affiliation add com.example.org1 -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client


echo "2.生成Admin@example.com的tls"
echo "1）注册Admin@example.com"
fabric-ca-client register --id.name Admin@org1.example.com --id.type client --id.affiliation "com.example.org1" --id.attrs '"hf.Registrar.Roles=client,orderer,peer,user","hf.Registrar.DelegateRoles=client,orderer,peer,user",hf.Registrar.Attributes=*,hf.GenCRL=true,hf.Revoker=true,hf.AffiliationMgr=true,hf.IntermediateCA=true,role=admin:ecert' --id.secret=123456 --csr.cn=org1.example.com --csr.hosts=['org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
echo "2）登记Admin@example.com"
fabric-ca-client enroll -d --enrollment.profile tls -u http://Admin@org1.example.com:123456@localhost:8056 --csr.cn=org1.example.com --csr.hosts=['org1.example.com']  -M ./crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tlsca --home ./fabric-ca-client
echo "3）生成tls"
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./intermediacatls2/ca-chain.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/ca.crt
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tlsca/signcerts/cert.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.crt
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tlsca/keystore
keystore=`ls -l $path | tail -n 1 | awk '{print $9}'`
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tlsca/keystore/$keystore ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.key



echo "3.生成peer0.org1.example.com的tls"
echo "1）注册peer0.org1.example.com"
fabric-ca-client register --id.name peer0.org1.example.com --id.type peer --id.affiliation "com.example.org1" --id.attrs '"role=peer",ecert=true' --id.secret=123456 --csr.cn=peer0.org1.example.com --csr.hosts=['peer0.org1.example.com'] -M ./crypto-config/peerOrganizations/org1.example.com/tlsca -u http://admin2:adminpw2@localhost:8056 --home ./fabric-ca-client
echo "2）登记peer0.org1.example.com"
fabric-ca-client enroll -d --enrollment.profile tls -u http://peer0.org1.example.com:123456@localhost:8056 --csr.cn=peer0.org1.example.com --csr.hosts=['peer0.org1.example.com']  -M ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tlsca --home ./fabric-ca-client
echo "3）生成tls"
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./intermediacatls2/ca-chain.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tlsca/signcerts/cert.pem ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
path=./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tlsca/keystore
keystore=`ls -l $path | tail -n 1 | awk '{print $9}'`
cp ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tlsca/keystore/$keystore ./fabric-ca-client/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key



#修改地方
#1.加config.yaml文件,2地方
#2.cd ca-server
#3.删除
#echo "NodeOUs:"&>$path
#echo "    Enable: true"&>>$path
#echo "    ClientOUIdentifier:"&>>$path
#echo "      Certificate: intermediatecerts/localhost-7056.pem"&>>$path
#echo "      OrganizationalUnitIdentifier: client"&>>$path
#echo "    PeerOUIdentifier:"&>>$path
#echo "      Certificate: intermediatecerts/localhost-7056.pem"&>>$path
#echo "      OrganizationalUnitIdentifier: peer"&>>$path
#4.删除peer1.org1
#5.替换tlsca